package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.Coin;
import com.gaogzhen.dto.CoinDto;
import com.gaogzhen.feign.CoinFeign;
import com.gaogzhen.model.ExceptionEnum;
import com.gaogzhen.service.CoinService;
import com.gaogzhen.utils.R;
import com.gaogzhen.utils.SqlExecuteUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @version 1.0
 * @description 数字货币控制器
 * @date 2022-11-04 10:14
 */
@RestController
@RequestMapping("/coins")
@Api(tags = "数字货币控制器")
public class CoinController implements CoinFeign {
    @Autowired
    private CoinService coinService;

    @GetMapping
    @ApiOperation("分页查询数字货币列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "数字货币标题"),
            @ApiImplicitParam(name = "name", value = "数字货币名称"),
            @ApiImplicitParam(name = "type", value = "数字货币类型"),
            @ApiImplicitParam(name = "walletType", value = "钱包类型"),
            @ApiImplicitParam(name = "status", value = "数字货币状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
    })
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<Page<Coin>> findByPage(String title, String name, String type, String walletType, Byte status, @ApiIgnore Page<Coin> page) {
        return R.ok(coinService.findByPage(title, name, type, walletType, status, page));

    }

    @GetMapping("/info/{id}")
    @ApiOperation("查询指定数字货币")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数字货币id"),
    })
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<Coin> info(@PathVariable("id") Long id) {
        return R.ok(coinService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增数字货币类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coin", value = "coin json数据")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_create')")
    public R<Coin> add(@RequestBody @Validated Coin coin) {
        coin.setStatus((byte)1);
        SqlExecuteUtils.sqlExceptionHandler(a ->coinService.save(a), coin, ExceptionEnum.COIN_TYPE_ADD_FAIL);

        return R.ok(coin);
    }

    @PatchMapping
    @ApiOperation("修改币种")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coin", value = "coin json数据")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R update(@RequestBody @Validated Coin coin) {
        SqlExecuteUtils.sqlExceptionHandler(a ->coinService.updateById(a), coin, ExceptionEnum.COIN_TYPE_UPDATE_FAIL);

        return R.ok();
    }

    @PatchMapping("/setStatus")
    @ApiOperation("设置数字货币类型状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "coinType唯一标识"),
            @ApiImplicitParam(name = "status", value = "coinType的状态"),
    })
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R setStatus(@RequestBody @ApiIgnore Coin coin) {
        SqlExecuteUtils.sqlExceptionHandler(a -> coinService.updateById(coin), coin, ExceptionEnum.COIN_SET_STATUS_FAIL);
        return R.ok();
    }

    @GetMapping("/all")
    @ApiOperation("查询所有的数字货币")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态")
    })
//    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<List<Coin>> findAll(@RequestParam(required = true) Byte status) {
        return R.ok(coinService.findAll(status));
    }

    @Override
    public Map<Long, CoinDto> findCoins(List<Long> coinIds) {

        return coinService.findCoins(coinIds);
    }
}
